using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;


public class TestLiHuaAnimator : MonoBehaviour
{
    public Animator animator;

    public Animator attacker_animator;

    // private BdPackage.BdTools.TimeIntervalUtil attack_timer = new BdPackage.BdTools.TimeIntervalUtil(0.5f, is_run_when_first_start: true);

    [Header("攻击间隔")]
    public float attack_time_interval = 0.1f;
    private float last_attack_time = -1f;
    
    public void btn__on_beat(bool call_lihua = true, bool call_attacker = true)
    {
        string state_name = "beat";
        // animator.Play("stop");
        if (call_lihua)
        {
            BdPackage.BdUtils.log("state_name:", state_name);
            BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
            // animator.Play(state_name);
            animator.SetBool(state_name, true);
        }
        if (call_attacker)
            attacker_animator.SetBool(state_name, true);
    }

    public void btn__set_lihua_direction(bool is_right_direction = false)
    {
        var new_rotation = my_utils.TransformUtils.RotateTransformByDirection(
            animator.transform, is_right_direction: is_right_direction);
        animator.transform.rotation = new_rotation;
    }

    public bool btn__get_obj_direction(GameObject obj)
    {
        bool is_right_direction = obj.transform.rotation.y % 360 >= 90 ? true : false;
        return is_right_direction;
    }

    public void btn__on_shift(bool is_right_direction = false)
    {
        btn__set_lihua_direction(is_right_direction);
        string state_name = "shift";
        animator.SetTrigger(state_name);
    }

    public void btn__only_attack(bool is_right_direction = false)
    {
        attacker_animator.GetComponent<CreateEffects>().is_hit_target = false;
        var attacker_trigger = is_right_direction ? "attack_left" : "attack_right";
        attacker_animator.SetBool(attacker_trigger, true);
    }
    
    public void btn__on_BeHit(bool is_right_direction = false, bool is_hit_target = true)
    {
        if (Time.time - last_attack_time < attack_time_interval)
            return;
        last_attack_time = Time.time;
        
        string state_name = "BeHit";
        
        // var new_rotation = my_utils.TransformUtils.RotateTransformByDirection(
        //     animator.transform, is_right_direction: is_right_direction);
        // animator.transform.rotation = new_rotation;
        btn__set_lihua_direction(is_right_direction);
        
        animator.Play("stop");
        
        BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
        animator.SetBool(state_name, true);

        attacker_animator.GetComponent<CreateEffects>().is_hit_target = is_hit_target;

        var attacker_trigger = is_right_direction ? "attack_left" : "attack_right";
        attacker_animator.SetBool(attacker_trigger, true);
    }

    public void btn__on_shift_0()
    {
        animator.Play("shift_0");
    }
    
    public void btn__on_shift_1()
    {
        animator.Play("shift_1");
    }
    
    private void Update()
    {
        // string state_name = "stop";
        //
        // if (Input.GetKeyDown(KeyCode.S))
        // {
        //     btn__on_beat();
        // }
        //
        //
        // if (Input.GetKeyDown(KeyCode.D))
        // {
        //     btn__on_BeHit(is_right_direction: false);
        // }
        //
        // if (Input.GetKeyDown(KeyCode.A))
        // {
        //     btn__on_BeHit(is_right_direction: true);
        // }
        //
        // if (Input.GetKeyDown(KeyCode.Q))
        // {
        //     btn__on_BeHit(is_right_direction: true, is_hit_target: false);
        // }
        //
        // if (Input.GetKeyDown(KeyCode.E))
        // {
        //     btn__on_BeHit(is_right_direction: false, is_hit_target: false);
        // }
    }

    [Header("启动GUI按钮")]
    public bool use_gui_debug_btns = false;
    private void OnGUI()
    {
        if (!use_gui_debug_btns)
            return;
        
        string state_name = "idle";
        if (GUILayout.Button($"state_name[{state_name}]"))
        {
            BdPackage.BdUtils.log("state_name:", state_name);
            BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
            animator.Play(state_name);
        }

        state_name = "stop";
        if (GUILayout.Button($"state_name[{state_name}]"))
        {
            BdPackage.BdUtils.log("state_name:", state_name);
            BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
            animator.Play(state_name);
        }
        state_name = "shift_0";
        if (GUILayout.Button($"state_name[{state_name}]"))
        {
            BdPackage.BdUtils.log("state_name:", state_name);
            BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
            animator.Play(state_name);
        }
        state_name = "shift_1";
        if (GUILayout.Button($"state_name[{state_name}]"))
        {
            BdPackage.BdUtils.log("state_name:", state_name);
            BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
            animator.Play(state_name);
        }
        
        // state_name = "beat";
        // if (GUILayout.Button($"state_name[{state_name}]")|| Input.GetKeyDown(KeyCode.S))
        // {
        //     BdPackage.BdUtils.log("state_name:", state_name);
        //     BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
        //     // animator.Play(state_name);
        //     animator.SetBool(state_name, true);
        //     attacker_animator.SetBool(state_name, true);
        // }
        //
        // state_name = "shift";
        // if (GUILayout.Button($"state_name[{state_name}]"))
        // {
        //     BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
        //     // BdPackage.BdUtils.log("state_name:", state_name);
        //     animator.Play(state_name);
        // }
        //
        // state_name = "BeHit";
        // if (GUILayout.Button($"state_name[{state_name}]") || Input.GetKeyDown(KeyCode.D))
        // {
        //     var new_rotation = my_utils.TransformUtils.RotateTransformByDirection(
        //         animator.transform, is_right_direction: false);
        //     animator.transform.rotation = new_rotation;
        //     
        //     BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
        //     animator.SetBool(state_name, true);
        //     attacker_animator.SetBool("attack_right", true);
        // }
        //
        // if (Input.GetKeyDown(KeyCode.A))
        // {
        //     var new_rotation = my_utils.TransformUtils.RotateTransformByDirection(
        //         animator.transform, is_right_direction: true);
        //     animator.transform.rotation = new_rotation;
        //     
        //     BdPackage.BdUtils.assert(BdTools.has_animation_state(animator, state_name), $"`animator`没有`state_name[{state_name}]`?");
        //     animator.SetBool(state_name, true);
        //     // animator.
        //     attacker_animator.SetBool("attack_left", true);
        // }
    }
    
    public static TestLiHuaAnimator instance;
    
    private void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

}
